define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {

        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'microend/paike/index',
                    // add_url: 'microend/paike/add',
                    edit_url: 'microend/paike/edit',
                    del_url: 'microend/paike/del',
                    multi_url: 'microend/paike/multi',
                    edit_user_url:'microend/paike/edit_user',
                    upper:'microend/paike/upper',
                    finance:'microend/paike/finance',
                    task_info:'microend/paike/task_info',
                    isDisable:"microend/paike/isDisable",
                    ajax_url:{
                        btn_1:{data:{'field':"status"},url:"microend/paike/isDisable",fc:function(data){
                            //Layer.msg();
                            return true;
                        }},
                        btn_2:{data:{'field':"role"},url:"microend/paike/isGroup",fc:function(data){
                                return true;
                            }},
                        btn_3:{data:{'field':"black"},url:"microend/paike/isBlack",fc:function(data){
                                return true;
                            }},
                        btn_4:'microend/paike/edit_balance?type=1',
                        btn_5:'microend/paike/edit_account?type=2',
                        btn_6:'microend/paike/edit_taobao?type=3',
                        btn_7:"microend/paike/edit_pass?type=4"
                    },
                    btn_mall_window:{
                        btn_array:["立即提交","取消"],
                        btn_evn_array_length:2,
                        btn_evn_array: {
                            evn_1:function (index,layero) {
                                var frame = Layer.getChildFrame('html', index);
                                var form = $("form",frame);
                                var success = function (data,ret) {
                                    Layer.msg(data.responseJSON.msg);
                                    table.bootstrapTable('refresh');
                                    Layer.close(index);
                                }
                                Fast.api.form_ajax_submit(form,success);
                            },
                            evn_2:function (index,layero) {
                                Layer.close(index);
                            }
                        },
                        area:['600px','400px']
                    },
                    table: 'user',
                },

            });

            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'uid',
                sortName: 'uid',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'a.username_alias', title: __('Username'),operate:'LIKE'},
                        {field: 'a.tao_bao_no', title: __('Tao_bao_no')},
                        {field: 'tao_bao_id', title: __('Tao_bao_id')},
                        {field: 'a.mobile', title: __('Mobile')},
                        {field: 'role', title: __('Role'),searchList: {"grouper":"组长","user":"组员"}, formatter: Table.api.formatter.normal},
                        {field: 'user_count', title: __('已有组员数'), operate:'BETWEEN'},
                        {field: 'person_count', title: __('Person_count'), operate:'BETWEEN'},
                        {field: 'task_count', title: __('Task_count'), operate:'BETWEEN'},
                        {field: 'month_task_count', title: __('Month_task_count'), operate:'BETWEEN'},
                        {field: 'balance', title: __('Balance'), operate:'BETWEEN'},
                        {field: 'task_limit_cash', title: __('Task_limit_cash'), operate:'BETWEEN'},
                        {field: 'e.single_status', title: __('Single_status'),searchList: {"1":"可接单","2":"不可接单"},formatter: Table.api.formatter.normal},
                        {field: 'e.cash_status', title: __('Cash_status'), searchList: {"1":"可以","2":"不可以"},formatter: Table.api.formatter.normal},
                        {field: 'l.real_status', title: __('派克认证状态'), searchList: {"1":"通过中","2":"认证通过","3":"认证失败","4":"禁用"},formatter: Table.api.formatter.normal},
                        {field: 'status', title: __('Status'),searchList: {"1":"启用","0":"禁用"},formatter: Table.api.formatter.toggle},
                        {field: 'd.auth_status',
                                title: __('Auth_status'),
                                searchList: {"1":"审核中","2":"审核通过","3":"审核不通过"},
                                //operate:'BETWEEN',
                                formatter:function(value,row,index){
                                    var keys = typeof this.searchList === 'object' ? Object.keys(this.searchList) : [];
                                    var index = keys.indexOf(value);
                                    value = value === null ? '未提交' : value.toString();
                                    var display = index > -1 ? this.searchList[value] : null;
                                    var icon = typeof this.icon !== 'undefined' ? this.icon : null;
                                    if (!display) {
                                        display = __(value.charAt(0).toUpperCase() + value.slice(1));
                                    }
                                    var html = '<span class="text">' + (icon ? '<i class="' + icon + '"></i> ' : '') + display + '</span>';
                                    if (this.operate != false) {
                                        html = '<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', display) + '" data-field="' + this.field + '" data-value="' + value + '">' + html + '</a>';
                                    }
                                    return html;
                            }},
                        {field: 'black', title: __('Black'), searchList: {"0": "否","1":"是"}, formatter: Table.api.formatter.toggle},
                        {field: 'e.task_status', title: __('Task_status'),searchList: {"1":"展示任务","2":"隐藏任务"}, formatter: Table.api.formatter.normal},
                        {field: 'e.account_status', title: __('账户是否正常'),searchList: {"1":"是","2":"否"},formatter: Table.api.formatter.normal},
                        {field: 'task_finish_count', title: __('Task_finish_count'),operate:'BETWEEN',formatter: Table.api.formatter.normal},
                        {field: 'task_day_count', title: __('Task_day_count'),operate:'BETWEEN',formatter: Table.api.formatter.normal},
                        {field: 'healthy', title: __('Healthy'), searchList: {"0":__('0'),"1":__('1'),"2":__('2'),"3":__('3')}, formatter: Table.api.formatter.normal},
                        {field: 'reg_time', title: __('Reg_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'group_created_time', title: __('成为组长时间'),operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter:function(value,row,index) {

                                try {
                                    var table = this.table;
                                    // 操作配置
                                    var options = table ? table.bootstrapTable('getOptions') : {};
                                    // 默认按钮组
                                    var buttons = $.extend([], this.buttons || []);

                                    if (options.extend.dragsort_url !== '') {
                                        buttons.push({
                                            name: 'dragsort',
                                            icon: 'fa fa-arrows',
                                            title: __('Drag to sort'),
                                            extend: 'data-toggle="tooltip"',
                                            classname: 'btn btn-xs btn-primary btn-dragsort'
                                        });
                                    }
                                    if (options.extend.edit_url !== '') {
                                        buttons.push({
                                            name: 'edit',
                                            //icon: 'fa fa-pencil',
                                            text:"编辑",
                                            title: __('Edit'),
                                            extend: 'data-toggle="tooltip"',
                                            classname: 'btn btn-xs btn-success btn-editone',
                                            url: options.extend.edit_url
                                        });
                                    }

                                    if (options.extend.upper !== '') {
                                        buttons.push(
                                            {
                                                name: 'upper',
                                                //icon: 'fa fa-trash',
                                                text: "上下线",
                                                title: "上下线",
                                                extend: 'data-toggle="tooltip"',
                                                classname: 'btn btn-xs btn-success btn-magic btn-editone1',
                                                url: options.extend.upper
                                            }
                                        );
                                    }
//

                                    if (options.extend.finance !== '') {
                                        buttons.push(
                                            {
                                                name: 'finance',
                                                //icon: 'fa fa-trash',
                                                text:"往来明细",
                                                title: "往来明细",
                                                extend: 'data-toggle="tooltip"',
                                                classname: 'btn btn-xs btn-success btn-magic btn-editone2',
                                                url: options.extend.finance
                                            }
                                        );
                                    }
                                    if (options.extend.task_info !== '') {
                                        buttons.push(
                                            {
                                                name: 'task_info',
                                                text:"任务列表",
                                                //icon: 'fa fa-trash',
                                                title: "任务列表",
                                                extend: 'data-toggle="tooltip"',
                                                classname: 'btn btn-xs btn-success btn-magic btn-editone3',
                                                url: options.extend.task_info
                                            }
                                        );
                                    }
                                    buttons.push(
                                        {
                                            name: 'is_disable',
                                            text: row.status == 1 ? __('Jin_yong') : __('Qi_yong'),
                                            extend: 'data-toggle="tooltip" data-name="btn_1"',
                                            classname: 'btn btn-xs btn-success btn-magic btn-ajax ',
                                            url:options.extend.isDisable
                                        },
                                        {
                                            name: 'is_group',
                                            text: row.role == "grouper" ? "组员" : "组长",
                                            extend: 'data-toggle="tooltip" data-name="btn_2"',
                                            classname: 'btn btn-xs btn-success btn-magic btn-ajax ',
                                            url:options.extend.ajax_url.btn_2.url
                                        },
                                        {
                                            name: 'is_black',
                                            text: row.black == 1 ? "解开黑名单" : "设置黑名单",
                                            extend: 'data-toggle="tooltip" data-name="btn_3"',
                                            classname: 'btn btn-xs btn-success btn-magic btn-ajax ',
                                            url:options.extend.ajax_url.btn_3.url
                                        },
                                        {
                                            name: 'edit_account',
                                            text: "账户修改",
                                            extend: 'data-toggle="tooltip" data-name="btn_5" data-title="账户修改" ',
                                            classname: 'btn btn-xs btn-success btn-magic btn-small-window',
                                            url:options.extend.ajax_url.btn_5
                                        },
                                        {
                                            name: 'edit_taobao',
                                            text: "绑定淘宝ID",
                                            extend: 'data-toggle="tooltip" data-name="btn_6" data-title="绑定淘宝ID" ',
                                            classname: 'btn btn-xs btn-success btn-magic btn-small-window',
                                            url:options.extend.ajax_url.btn_6
                                        },
                                        {
                                            name: 'edit_pass',
                                            text: "重置密码",
                                            extend: 'data-toggle="tooltip" data-name="btn_7" data-title="重置密码" ',
                                            classname: "btn btn-xs btn-success btn-magic btn-small-window ",
                                            url:options.extend.ajax_url.btn_7
                                        }
                                    );

                                    buttons.push({
                                        name: 'edit_balance',
                                        text: "账户余额修改",
                                        extend: 'data-toggle="tooltip" data-name="btn_4" data-title="账户余额修改" ',
                                        classname: 'btn btn-xs btn-success btn-magic btn-small-window',
                                        url: options.extend.ajax_url.btn_4
                                    });

                                    return Table.api.buttonlink(this, buttons, value, row, index, 'operate');

                            }catch (err){
                                    console.log(err)
                                    }
                            }
                        }
                    ]
                ]
            });
            
            // 为表格绑定事件
            Table.api.bindevent(table);

            //批量启用
            $(document).on("click", "#mult_pass", function (e){
                var data = table.bootstrapTable('getSelections');
                var table_select = JSON.stringify(table.bootstrapTable('getSelections'));
                layer.confirm('确定全部启用？',{
                        btn: ['确定','取消'] //可以无限个按钮
                    },function(index, layero){
                        Fast.api.ajax({
                            url: 'microend/paike/mult_pass',
                            data: {
                                'ids': data,

                            },
                            success: function (data) {
                                layer.msg(data.msg);

                                table.bootstrapTable("refresh");
                            },
                            error: function (data) {
                                //失败的回调
                                layer.msg(data.msg);
                                table.bootstrapTable("refresh");
                            }
                        });
                    },function(index, layero){
                        layer.msg('已取消操作。');
                    }
                );
            });


            //批量禁用
            $(document).on("click", "#mult_no", function (e){
                var data = table.bootstrapTable('getSelections');
                console.log(data);
                var table_select = JSON.stringify(table.bootstrapTable('getSelections'));
                layer.confirm('确定全部禁用？',{
                        btn: ['确定','取消'] //可以无限个按钮
                    },function(index, layero){
                        Fast.api.ajax({
                            url: 'microend/paike/mult_no',
                            data: {
                                'ids': data,

                            },
                            success: function (data) {
                                layer.msg(data.msg);
                                table.bootstrapTable("refresh");
                            },
                            error: function (data) {
                                //失败的回调
                                layer.msg(data.msg);
                                table.bootstrapTable("refresh");
                            }
                        });
                    },function(index, layero){
                        layer.msg('已取消操作。');
                    }
                );
            });



            //批量锁定金额
            $(document).on("click", "#mult_price", function (e){
                var data = table.bootstrapTable('getSelections');
                console.log(data);
                var table_select = JSON.stringify(table.bootstrapTable('getSelections'));
                layer.confirm('确定全部锁定金额？',{
                        btn: ['确定','取消'] //可以无限个按钮
                    },function(index, layero){
                        Fast.api.ajax({
                            url: 'microend/paike/mult_price',
                            data: {
                                'ids': data,

                            },
                            success: function (data) {
                                layer.msg(data.msg);
                                table.bootstrapTable("refresh");
                            },
                            error: function (data) {
                                //失败的回调
                                layer.msg(data.msg);
                                table.bootstrapTable("refresh");
                            }
                        });
                    },function(index, layero){
                        layer.msg('已取消操作。');
                    }
                );
            });




            //批量解开金额
            $(document).on("click", "#mult_price_no", function (e){
                var data = table.bootstrapTable('getSelections');
                console.log(data);
                var table_select = JSON.stringify(table.bootstrapTable('getSelections'));
                layer.confirm('确定全部解开金额？',{
                        btn: ['确定','取消'] //可以无限个按钮
                    },function(index, layero){
                        Fast.api.ajax({
                            url: 'microend/paike/mult_price_no',
                            data: {
                                'ids': data,

                            },
                            success: function (data) {
                                layer.msg(data.msg);
                                table.bootstrapTable("refresh");
                            },
                            error: function (data) {
                                //失败的回调
                                layer.msg(data.msg);
                                table.bootstrapTable("refresh");
                            }
                        });
                    },function(index, layero){
                        layer.msg('已取消操作。');
                    }
                );
            });


        },
        add: function () {


            //批量解开金额
            $(document).on("click", ".reset_1", function (e){

            });
            Controller.api.bindevent();
        },
        edit: function () {
            $(function(){
                //console.log($(".example2"))
                require(['img_css/easy-loading','img_css/hammer.min','img_css/jquery.mousewheel.min','img_css/zoom-marker'],function(){
                    // $('#zoom-marker-img').zoomMarker({
                    //     src: $('#zoom-marker-img').data("url"),
                    //     rate: 0.2,
                    //     width: 100,
                    //     max: 1000,
                    //     markers:[
                    //         //{src:"img/marker.svg", x:500, y:500}
                    //     ]
                    // });
                    $("img").each(function(){
                        console.log($(this).data("url"));
                        if($(this).data("url")!=undefined && $(this).data("url")!="") {
                            $(this).zoomMarker({
                                src: $(this).data("url"),
                                rate: 0.1,
                                min: 100,
                                width: 100,
                                max: 3000,
                                markers: [
                                    //{src:"img/marker.svg", x:500, y:500}
                                ],
                            });
                        }
                    });
                    // $('#zoom-marker-img-alt').zoomMarker({
                    //     src: $('#zoom-marker-img-alt').data("url"),
                    //     rate: 0.1,
                    //     min:100,
                    //     width: 100,
                    //     max: 3000,
                    //     markers:[
                    //         //{src:"img/marker.svg", x:500, y:500}
                    //     ]
                    //
                    //
                    // });
                })
            });

            var table = $("#table");
            Controller.api.bindevent();
            var _layer = parent.Layer;
            var _index = _layer.getFrameIndex(window.name);
            $("#pass").on("click", function () {
                $.ajax({
                                type: "post",
                                url: 'microend/paike/real',
                                async: true,
                                dataType: "json",
                                data: getData(2),
                                success: function (data) {
                                    if (data.status == 1) {
                                        Layer.msg(data.msg, {icon: 6, time: 1500, shade: 0.1}, function (index) {
                                            _layer.close(_index);
                                            table.bootstrapTable('refresh');
                                        });
                                    }
                                    else{
                                        Layer.msg(data.msg, {icon: 5, time: 1500, shade: 0.1}, function (index) {
                                            _layer.close(_index);
                                            table.bootstrapTable('refresh');
                                        });
                                    }
                                },
                                error: function () {
                                    Layer.msg(data.msg, {icon: 5, time: 1500, shade: 0.1});
                                    _layer.close(_index);
                                    table.bootstrapTable('refresh');
                                }
                    });
            });
            $("#fail").on("click", function () {
                $.ajax({
                    type: "post",
                    url: 'microend/paike/real',
                    async: true,
                    dataType: "json",
                    data: getData(3),
                    success: function (data) {
                        if (data.status == 1) {
                            Layer.msg(data.msg, {icon: 6, time: 1500, shade: 0.1}, function (index) {
                                _layer.close(_index);
                                table.bootstrapTable('refresh');
                            });
                        }
                        else{
                            Layer.msg(data.msg, {icon: 5, time: 1500, shade: 0.1}, function (index) {
                                _layer.close(_index);
                                table.bootstrapTable('refresh');
                            });
                        }
                    },
                    error: function () {
                        Layer.msg(data.msg, {icon: 5, time: 1500, shade: 0.1});
                        _layer.close(_index);
                        table.bootstrapTable('refresh');
                    }
                });
            });
            function getData(t) {
                var data = {};
                data.real_id = $("#real_id").val();
                data.after_safe_img = $("#c-local-1").val();
                data.tao_gas_img = $("#c-local-2").val();
                data.tao_flower_img = $("#c-local-3").val();
                data.tao_exam_img = $("#c-local-4").val();
                data.tao_baby_img = $("#c-local-5").val();
                data.tao_evaluate_img = $("#c-local-6").val();
                data.real_status = t;
                data.uid = $("#uid").val();
                return data;
            }

        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        },
        upper:function(){
            var ids = $("input[name='ids']").val();
            console.log(ids);
            Table.api.init({
                extend: {
                    index_url: 'microend/paike/upper?ids='+ids,
                    table: 'user_tree',
                }
            });
            var table = $("#table_upper");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'node_id',
                sortName: 'node_id',
                columns: [
                    [
                        {field: 'uid', title: "ID"},
                        {field: 'username', title: "姓名"},
                        {field: 'mobile', title: "手机"},
                        {field: 'tao_bao_no', title: "淘宝号"},
                        {field: 'role', title: "角色",searchList: {"grouper":"组长","user":"组员"}, formatter: Table.api.formatter.normal},
                        {field: 'real_status', title: "派克状态", operate:'BETWEEN'}
                    ]
                ]
            });


        },
        finance:function(){
            var ids = $("input[name='ids']").val();
            console.log(ids);
            Table.api.init({
                extend: {
                    index_url: 'microend/paike/finance?ids='+ids,
                    table: 'finance',
                }
            });
            var table = $("#table_finance");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'fina_id',
                sortName: 'fina_id',
                columns: [
                    [
                        {field: 'order_sno', title: "订单编号"},
                        {field: 'fina_cash', title: "操作金额"},
                        {field: 'balance', title: "当前金额"},
                        {field: 'obj_type', title: "操作类型"},
                        {field: 'on_time', title: "操作时间", operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'remark', title: "备注"}
                    ]
                ]
            });
        },
        task_info:function(){
            var ids = $("input[name='ids']").val();
            console.log(ids);
            Table.api.init({
                extend: {
                    index_url: 'microend/paike/task_info?ids='+ids,
                    table: 'shop_order',
                }
            });
            var table = $("#table_task_info");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'order_id',
                sortName: 'order_id',
                columns: [
                    [
                        {field: 'order_sno', title: "订单编号"},
                        {field: 'create_time', title: "接单时间",operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'task_type', title: "订单类型",searchList: {
                                "1":__('精刷单'),
                                "2":__('隔日单'),
                                "3":__('流量单')
                            },formatter:function(value,row,index){
                                var keys = typeof this.searchList === 'object' ? Object.keys(this.searchList) : [];
                                var index = keys.indexOf(value.toString());
                                value = value === null ? '' : value.toString();
                                var display = index > -1 ? this.searchList[value] : null;
                                var icon = typeof this.icon !== 'undefined' ? this.icon : null;
                                if (!display) {
                                    display = __(value.charAt(0).toUpperCase() + value.slice(1));
                                }
                                var html = '<span class="text">' + (icon ? '<i class="' + icon + '"></i> ' : '') + display + '</span>';

                                return html;
                            }},
                        {field: 'shop_name', title: "店铺名称"},
                        {field: 'real_money', title: "金额"},
                        {field: 'beat_price', title: "平台录入金额"},
                        {field: 'status', title: "任务状态",searchList: {

                                "0":__('已拍下'),
                                "1":__('未审核'),
                                "2":__('已审核'),
                                "3":__('已上传'),
                                "4":__('已复查'),
                                "5":__('已付款'),
                                "6":__('已下单'),
                                "7":__('待发货'),
                                "8":__('待好评'),
                                "9":__('已发货'),
                                "10":__('已好评'),
                                "11":__('待追评'),
                                "12":__('已追评'),
                                "13":__('已完成'),
                            },formatter:function(value,row,index){
                                var keys = typeof this.searchList === 'object' ? Object.keys(this.searchList) : [];
                                if(value==undefined || value=="null"){
                                    value =   value
                                }else{
                                    value = value.toString();
                                }
                                var index = keys.indexOf(value);
                                value = value === null ? '' : value;
                                var display = index > -1 ? this.searchList[value] : null;
                                var icon = typeof this.icon !== 'undefined' ? this.icon : null;
                                if (!display) {
                                    display = __(value.charAt(0).toUpperCase() + value.slice(1));
                                }
                                var html = '<span class="text">' + (icon ? '<i class="' + icon + '"></i> ' : '') + display + '</span>';

                                return html;
                            }},
                        {field: 'finished_time', title: "返佣时间", operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime}
                    ]
                ]
            });
        }

    };
    return Controller;
});